﻿using System;
using System.Collections.Generic;
using System.Text;
using ExpressionUtil.Common;

namespace ExpressionUtil.Parser.Handler
{
    /// <summary>
    /// 左括号处理程序
    /// </summary>
    internal class BracketFunctionHandler : ParseHandlerBase
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public BracketFunctionHandler()
        {
            this.next = new BracketPairHandler();
        }

        /// <summary>
        /// 解析元素
        /// </summary>
        /// <param name="context">解析参数</param>
        /// <param name="element">需要解析的元素</param>
        /// <returns>是否已解析</returns>
        protected override bool ParseCore(string element, ParseContext context)
        {
            bool ret = false;
            if (element.EndsWith("("))
            {
                char prevChar = context.Scanner.PrevChar;
                if (element.Length == 1 && prevChar != char.MaxValue && !StringHelper.IsOperatorChar(prevChar) && !StringHelper.IsSpecialChar(prevChar))
                {
                    throw new FormatException("Missing operator before left bracket.");
                }
                //左括弧，函数调用
                context.OperatorStack.Push(element);
                if (element.Length > 1)
                {
                    context.FunctionParameterCount.Push(0);
                }
                ret = true;
            }
            return ret;
        }
    }
}
